<template>
  <header class="header">
    <!-- 主导航栏 -->
    <div class="container">
      <div class="header-content">
        <!-- Logo -->
        <div class="logo">
          <router-link to="/">
            <span class="logo-text">{{ headerData?.ShopName || '商城' }}</span>
          </router-link>
        </div>

        <!-- 搜索框 -->
        <div class="search-box">
          <div class="custom-search-box">
            <el-dropdown @command="handleSearchTypeChange" trigger="click" class="search-dropdown">
              <div class="search-type-button">
                <span class="search-type-text">{{ searchTypeText }}</span>
                <el-icon class="dropdown-arrow"><ArrowDown /></el-icon>
              </div>
              <template #dropdown>
                <el-dropdown-menu class="search-dropdown-menu">
                  <el-dropdown-item command="Sp" :class="{ 'is-active': searchType === 'Sp' }">
                    <el-icon><Box /></el-icon>
                    <span>產品</span>
                  </el-dropdown-item>
                  <el-dropdown-item command="DD" :class="{ 'is-active': searchType === 'DD' }">
                    <el-icon><Document /></el-icon>
                    <span>訂單</span>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
            <input
                v-model="searchKeyword"
                type="text"
                class="search-input"
                :placeholder="searchType === 'Sp' ? '搜索產品' : '搜索訂單'"
                @keyup.enter="handleSearch"
            />
            <button class="search-button" @click="handleSearch">
              <el-icon color="#d1d1d1" size="15"><Search /></el-icon>
            </button>
          </div>
        </div>

        <!-- 右侧工具栏 -->
        <div class="header-tools">
          <router-link to="/cart" class="mobile-cart">
            <el-icon size="30" color="#d1d1d1">
              <ShoppingCart />
            </el-icon>
            <span class="cart-count" v-if="cartStore.totalItems > 0">{{ cartStore.totalItems }}</span>
          </router-link>
        </div>
      </div>
    </div>

    <!-- 导航菜单 -->
    <div class="nav-menu">
      <div class="container">
        <div class="menu-content">
          <!-- 导航链接 -->
          <nav class="nav-links">
            <router-link to="/" class="nav-link" exact-active-class="router-link-active">首頁</router-link>

            <router-link v-for="category in headerData?.TypeList || []"
                         :key="category.TypeId"
                         :to="`/category/${category.TypeId}`"
                         class="nav-link"
                         active-class="router-link-active">
              {{ category.TypeName }}
            </router-link>
          </nav>
        </div>
      </div>
    </div>
  </header>
</template>

<script setup lang="ts">
import { onMounted, ref, computed} from 'vue'
import {useRouter} from 'vue-router'
import {ElMessage} from 'element-plus'
import {Search, ShoppingCart, ArrowDown, Box, Document} from '@element-plus/icons-vue'
import {useCartStore} from '@/store'
import {fetchHeader} from "@/request/req.ts";
import {commonStore} from '@/store/common.ts'

const router = useRouter()
const cartStore = useCartStore()
const common = commonStore()

const searchKeyword = ref('')
const searchType = ref('Sp')

// 计算搜索类型显示文本
const searchTypeText = computed(() => {
  return searchType.value === 'Sp' ? '產品' : '訂單'
})

// 处理搜索类型变更
const handleSearchTypeChange = (command: string) => {
  searchType.value = command
}

// 添加 TypeList 数据的响应式变量
const headerData = ref<{
  ShopName: string;
  TypeList: Array<{
    TypeId: number;
    TypeName: string;
    ParentId: number;
  }>;
  ShopPhone: string;
  ServerUrl: string;
} | null>(null)

const getHeaderData = async () => {
  try {
    const data = await fetchHeader()
    headerData.value = data;

    if (data) {
      common.addItem({
        id: 1,
        name: 'url',
        value: data.ServerUrl || '',
        createdAt: new Date()
      });
      common.addItem({
        id: 2,
        name: 'ShopPhone',
        value: data.ShopPhone,
        createdAt: new Date()
      });
      common.addItem({
        id: 3,
        name: 'Currency',
        value: data.Currency || '',
        createdAt: new Date()
      });
      common.addItem({
        id: 4,
        name: 'UnifyNumber',
        value: data.UnifyNumber,
        createdAt: new Date()
      });
    }
  } catch (error) {
    console.error('Failed to fetch header data:', error)
    // 设置默认值
    common.addItem({
      id: 2,
      name: 'ShopPhone',
      value: 'undefined',
      createdAt: new Date()
    });
  }
}

// 搜索功能
const handleSearch = () => {
  if (!searchKeyword.value.trim()) {
    ElMessage.warning('請輸入搜索关键词')
    return
  }

  // 根据搜索类型进行不同的路由跳转
  if (searchType.value === 'Sp') {
    // 產品搜索
    router.push({
      path: '/search',
      query: {keyword: searchKeyword.value,type:searchType.value}
    })
  } else {
    // 订单搜索
    router.push({
      path: '/order',
      query: {keyword: searchKeyword.value,type:searchType.value}
    })
  }
}


onMounted(() => {
  // 加载购物车数据
  cartStore.loadFromLocalStorage()
  // 获取產品分类
  getHeaderData()
})
</script>

<style scoped>
.header {
  background: var(--gradient-bg-primary);
  border-bottom: 2px solid var(--border-primary);
  box-shadow: var(--shadow-medium);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.top-bar .container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 30px;
  padding: 15px 0;
}

/* Logo */
.logo {
  flex-shrink: 0;
  min-width: 0;
}

.logo a {
  display: flex;
  align-items: center;
  text-decoration: none;
  color: var(--primary-color);
  font-size: 24px;
  font-weight: bold;
  min-width: 0;
  transition: var(--transition-all);
}

.logo a:hover {
  color: var(--success-color);
  transform: scale(1.05);
}

.logo-text {
  font-size: 30px;
  font-family: var(--font-family);
  font-weight: var(--font-weight-bold);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
  flex-shrink: 0;
  background: linear-gradient(135deg, var(--primary-color), var(--success-color));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* 搜索框 */
.search-box {
  flex: 1;
  max-width: 500px;
}

.search-input {
  width: 100%;
}

/* 自定义搜索框样式 */
.custom-search-box {
  display: flex;
  align-items: center;
  background: var(--bg-secondary);
  border: 2px solid var(--border-dark);
  border-radius: var(--radius-rounded);
  transition: var(--transition-all);
  height: 40px;
}

.custom-search-box:focus-within {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px var(--primary-bg);
  background: var(--bg-primary);
}

/* 下拉菜单样式 */
.search-dropdown {
  height: 100%;
}

.search-type-button {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border: none;
  color: var(--text-primary);
  padding: 0 12px;
  height: 100%;
  border-radius: var(--radius-rounded) 0 0 var(--radius-rounded);
  cursor: pointer;
  font-size: var(--font-size-xl);
  font-weight: var(--font-weight-medium);
  min-width: 80px;
}

.search-type-text {
  font-size: 14px;
  font-weight: 500;
}


:deep(.el-dropdown-menu) {
  background: var(--bg-secondary) !important;
  border: 2px solid var(--border-dark) !important;
  border-radius: var(--radius-rounded) !important;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.3) !important;
  padding: 4px !important;
  min-width: 120px !important;
}

:deep(.el-dropdown-menu__item) {
  display: flex !important;
  align-items: center !important;
  gap: 8px !important;
  padding: 10px 12px !important;
  color: var(--text-primary) !important;
  border-radius: 6px !important;
  margin: 2px 0 !important;
  transition: all 0.3s ease !important;
  font-weight: 500 !important;
  background: transparent !important;
}

:deep(.el-dropdown-menu__item:hover) {
  background: linear-gradient(135deg, rgba(0, 212, 255, 0.2) 0%, rgba(0, 153, 204, 0.2) 100%) !important;
  color: #00d4ff !important;
  transform: translateX(2px) !important;
}

:deep(.el-dropdown-menu__item:focus) {
  background: linear-gradient(135deg, rgba(0, 212, 255, 0.2) 0%, rgba(0, 153, 204, 0.2) 100%) !important;
  color: #00d4ff !important;
}

:deep(.el-dropdown-menu__item.is-active) {
  background: linear-gradient(135deg, rgba(0, 212, 255, 0.2) 0%, rgba(0, 153, 204, 0.2) 100%) !important;
  color: #ffffff !important;
  font-weight: bold !important;
}

.search-dropdown-menu {
  background: var(--bg-secondary);
  padding: 4px;
  min-width: 120px;
}

.search-input {
  flex: 1;
  background: transparent;
  border: none;
  color: #ecf0f1;
  padding: 0 12px;
  height: 100%;
  outline: none;
  font-size: 14px;
}

.search-input::placeholder {
  color: #95a5a6;
}

.search-button {
  background: linear-gradient(135deg, #00d4ff 0%, #0099cc 100%);
  border: none;
  height: 100%;
  border-radius: 0 25px 25px 0;
  cursor: pointer;
  transition: all 0.3s ease;
  width: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
}

.search-button:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 15px rgba(0, 212, 255, 0.3);
}

/* 右侧工具栏 */
.header-tools {
  display: flex;
  align-items: center;
  gap: 20px;
}

.mobile-cart {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  transition: all 0.3s ease;
  text-decoration: none;
}

.mobile-cart:hover {
  background: rgba(0, 212, 255, 0.2);
  border-color: #00d4ff;
  transform: scale(1.05);
  box-shadow: 0 4px 15px rgba(0, 212, 255, 0.3);
}

.cart-count {
  position: absolute;
  top: -5px;
  right: -5px;
  background: linear-gradient(135deg, #ff4757 0%, #ff3742 100%);
  color: #fff;
  font-size: 10px;
  font-weight: bold;
  padding: 2px 6px;
  border-radius: 50%;
  min-width: 16px;
  height: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  box-shadow: 0 2px 8px rgba(255, 71, 87, 0.3);
}

/* 导航菜单 */
.nav-menu {
  height: 50px;
}

.menu-content {
  display: flex;
  align-items: center;
  height: 100%;
}

.nav-links {
  display: flex;
  gap: 30px;
}

.nav-link {
  text-decoration: none;
  color: #ecf0f1;
  font-weight: 500;
  font-size: 14px;
  padding: 15px 20px;
  border-radius: 8px;
  transition: all 0.3s;
  position: relative;
}

.nav-link::after {
  content: '';
  position: absolute;
  bottom: 10px;
  left: 20px;
  width: 0;
  height: 2px;
  background: linear-gradient(135deg, #00d4ff, #00ff88);
  transition: width 0.3s ease;
}

.nav-link:hover {
  color: #00d4ff;
  background: rgba(0, 212, 255, 0.1);
}

.nav-link:hover::after,
.nav-link.router-link-active::after {
  width: calc(100% - 40px);
}

.nav-link.router-link-active {
  color: #00d4ff;
  background: rgba(0, 212, 255, 0.15);
}


/* 响应式设计 */
@media (max-width: 768px) {
  .container {
    padding: 0 12px;
  }

  .top-bar {
    display: none;
  }

  .header-content {
    flex-direction: row;
    gap: 12px;
    padding: 12px 0;
    align-items: center;
  }

  .logo {
    flex-shrink: 0;
  }

  .logo-text {
    font-size: 24px;
  }

  .search-box {
    flex: 1;
    max-width: none;
    order: 2;
  }

  .custom-search-box {
    height: 36px;
  }

  .search-type-button {
    font-size: 12px;
    padding: 0 8px;
    min-width: 60px;
  }

  .search-type-text {
    font-size: 12px;
  }

  .search-input {
    font-size: 13px;
    padding: 0 8px;
  }

  .search-button {
    width: 36px;
  }

  .header-tools {
    order: 3;
    gap: 12px;
  }

  .mobile-cart {
    width: 36px;
    height: 36px;
  }

  .nav-menu {
    display: none;
  }
}

@media (max-width: 480px) {
  .container {
    padding: 0 8px;
  }

  .header-content {
    gap: 8px;
    padding: 10px 0;
  }

  .logo-text {
    font-size: 20px;
  }

  .custom-search-box {
    height: 32px;
  }

  .search-type-button {
    font-size: 11px;
    padding: 0 6px;
    min-width: 50px;
  }

  .search-type-text {
    font-size: 11px;
  }

  .search-input {
    font-size: 12px;
    padding: 0 6px;
  }

  .search-button {
    width: 32px;
  }

  .mobile-cart {
    width: 32px;
    height: 32px;
  }

  .cart-count {
    font-size: 9px;
    padding: 1px 4px;
    min-width: 14px;
    height: 14px;
  }
}
</style>
